package com.hxf.web.action.user;

import java.io.IOException;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.hxf.common.DateUtil;
import com.hxf.common.GlobalMPURL;
import com.hxf.common.PublicUtil;
import com.hxf.dto.AppUserDto;
import com.hxf.dto.ReturnData;
import com.hxf.dto.UserDto;
import com.hxf.exception.ServiceException;
import com.hxf.web.action.WebActionBase;

/**
 * 前台注册会员
 * @author Administrator
 */

@Results({@Result(name = "success", location = "../common/success.jsp"), @Result(name = "error", location = "../common/error.jsp"), @Result(name = "msg", location = "../common/msg.jsp")})
public class AppUserAction extends WebActionBase{

	private static final long serialVersionUID = 1L;
	public List<AppUserDto> listAppUser;
	public AppUserDto appUserDto;
	
	private Integer id;
	private String userName;
	private String userPwd;
	private Integer userSex;
	private String realName;
	private String userIdNo;
	private String userPhone;
	private String userAddress;
	private String userZipCode;
	private String qq;
	private String userEmail;
	private Integer status;  //状态
	private String userIds;
	
	public String appUserList(){
		
		try{

			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
			
			AppUserDto appUserDto = new AppUserDto();
			if(PublicUtil.isNotNullAndEmpty(userName)){
				appUserDto.setUserName(userName);
			}
			
			if(PublicUtil.isNotNullAndEmpty(userPhone)){
				appUserDto.setUserPhone(userPhone);
			}
			
			//查询下级会员
			if(null != adminUserDto){
				appUserDto.setParentId(getAdminUser().getId());//根据当前登陆 IP 查找下级 会员
			}else if(null != restaurentUserDto){
				appUserDto.setParentId(getRestaurentUserDto().getId());//餐厅登陆，查找餐厅的会员
			}
			
			ReturnData<AppUserDto> data = super.serviceFacade.getAppUserService().findByAppUser(appUserDto, targetPage, pageSize);
			listAppUser = data.getResultlist();
			data.setRecordcount(listAppUser.size());
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return "app-user-list";
	}
	
	/**
	 * 添加 前台注册会员
	 * @return
	 */
	public String appUserAdd(){
		
		return "app-user-add";
	}
	
	public String appUserUpdate(){
		try{
			appUserDto = (AppUserDto)super.serviceFacade.getAppUserService().get(AppUserDto.class, id);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "app-user-update";
	}

	//添加或者修改
	public String userSaveOrUpdate() {
		try {
			//菜单权限验证 
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			}
			
			Object adminUserDto = getSession().getAttribute("adminUserDto"); //联盟管理员Session
			Object restaurentUserDto = getSession().getAttribute("restaurentUserDtoLogin");//餐厅 Session 
			
			AppUserDto ud = new AppUserDto();
			ud.setUserName(userName);//用户名
			ud.setRealName(realName);//真实姓名
			ud.setUserSex(userSex);//性别
			ud.setUserIdNo(userIdNo);//身份证号码
			ud.setUserPhone(userPhone);//电话
			ud.setUserAddress(userAddress);//地址
			ud.setUserZipCode(userZipCode);
			ud.setUserEmail(userEmail);//邮箱
			ud.setUserPwd(userPwd);//密码
			
			if(null != adminUserDto){
				ud.setParentId(getAdminUser().getId());//当前管理员登陆添加的会员
			}else if(null != restaurentUserDto){
				ud.setParentId(getRestaurentUserDto().getId());//餐厅添加的会员
			}
			
			if (id != null && !"null".equals(id)) {  

				ud.setId(id);
				super.serviceFacade.getAppUserService().update(ud);//修改
			} else {
				
				ud.setCreateTime(DateUtil.getCurrentDate(DateUtil.DATE_PATTERN_M));//当前时间
				super.serviceFacade.getAppUserService().save(ud);//保存
			}
			
			requestURL = "app-user!appUserList.html";
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		//return "app-user-list";
		
		//return "app-user!appUserList.html";
		return SUCCESS;
	}
	
	/**
	 * 删除用户信息
	 * @return
	 */
	public String appUserDelete() {
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			super.serviceFacade.getAppUserService().delete(appUserDto);
			super.println("success");
		} catch (ServiceException e) { 
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		} 
		return null;
	}
	
	/**
	 * 修改用户信息
	 * @return
	 */
	public String userUpdate() { 
		try {
			//菜单权限验证
			if (!menuPrivilegeValidate(GlobalMPURL.USER_PRIVILEGE_URL)) {
				return "msg";
			} 
			appUserDto = (AppUserDto) super.serviceFacade.getAppUserService().get(AppUserDto.class, id);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		}
		return "user-update";
	}
	
	/**
	 * 修改会员状态
	 * @return
	 */
	public String updateUserStatus() {
		//菜单权限验证
		if (!menuPrivilegeValidate(GlobalMPURL.APP_USER_URL)) {
			return "msg";
		}  
		try {
			if ("".equals(userIds) || null == userIds || status == null) {
				println(-1);
				return null;
			}
			super.serviceFacade.getAppUserService().updateUserStatus(userIds, status);
			println(0);
		} catch (ServiceException e) {
			logger.debug(e.getMessage(), e);
		} catch (IOException e) {
			logger.debug(e.getMessage(), e);
		}
		return null;
	}
	
	public AppUserDto getAppUserDto() {
		return appUserDto;
	}

	public void setAppUserDto(AppUserDto appUserDto) {
		this.appUserDto = appUserDto;
	}
	
	public List<AppUserDto> getListAppUser() {
		return listAppUser;
	}

	public void setListAppUser(List<AppUserDto> listAppUser) {
		this.listAppUser = listAppUser;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}

	public String getRealName() {
		return realName;
	}

	public void setRealName(String realName) {
		this.realName = realName;
	}

	public String getUserIdNo() {
		return userIdNo;
	}

	public void setUserIdNo(String userIdNo) {
		this.userIdNo = userIdNo;
	}

	public String getUserPhone() {
		return userPhone;
	}

	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}

	public String getUserAddress() {
		return userAddress;
	}

	public void setUserAddress(String userAddress) {
		this.userAddress = userAddress;
	}

	public String getUserZipCode() {
		return userZipCode;
	}

	public void setUserZipCode(String userZipCode) {
		this.userZipCode = userZipCode;
	}

	public String getUserEmail() {
		return userEmail;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public Integer getUserSex() {
		return userSex;
	}

	public void setUserSex(Integer userSex) {
		this.userSex = userSex;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getUserIds() {
		return userIds;
	}

	public void setUserIds(String userIds) {
		this.userIds = userIds;
	}

	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

}
